import { ILoadMapTypeAll, ISearchTranslateAll } from '@/common';
import { ManageCommonAPI } from '@/pages/type';
import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getLoadMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useMemo } from 'react';
import { useRequest } from 'umi';
import { getManageTabSwitch, getSearchList } from '../mconfig';
const baseUrl = getBaseUrl();
// const baseUrl = 'http://192.168.153.246:31019'; //旭潮本地
// const baseUrl = '/tempContractTest';

const templateManageService = (curRes: Record<string, any>) => {
  /**  需要获取的数据字典 */
  const loadMdicIds: string[] = [
    `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_STATE}`, //范本状态
    `${getTenantID()}${ILoadMapTypeAll.CONTRACT_TYPE}`, //范本类别
    `${getTenantID()}${ILoadMapTypeAll.CONTRACTTEMP_CANCELTYPE}`, //作废类型
  ];

  /** 获取数据字典loadMdicMap */
  const loadMdicMapObj: Record<string, any> = getLoadMapList(loadMdicIds);

  /**获取权限参数 */
  const queryTypeObj: Record<string, any> = getManageTabSwitch(curRes);

  /** 获取搜索参数 */
  const searchListParams = getSearchList(loadMdicMapObj?.loadMapInfo, queryTypeObj);

  /** 获取列表数据  */
  const { tableProps, data, loading, run, refresh } = useRequest(
    (opt: ManageCommonAPI.IGridRequestType) => {
      const requestBody: Record<string, any> = {
        ...queryTypeObj,
        current: 1,
        pageSize: 10,
        sort: 'createTime',
        order: 'desc',
        dicTypeIds: loadMdicIds?.toString(),
        ...opt,
      };

      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, requestBody, {
        dicTransName: 'state,businessType,templateType,cancelType',
        orgTransName: `${ISearchTranslateAll.SIGN_DEPT},${ISearchTranslateAll.CREATER_DEPT},${ISearchTranslateAll.OWNER_DEPT},${ISearchTranslateAll.CANCEL_DEPT}`,
        // areaTransName: `${ISearchTranslateAll.AREA_TRANS}`,
      });

      const requestURL = `${baseUrl}/contractTemplateController/dataGrid/${requestBody?.params}`;
      return requestFunComponFiex('POST', requestURL, searchParams);
    },
    {
      manual: false, //页面初始请求为 true
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /* 获取页面数据 */
  const getTempContractData = (otherRequest: ManageCommonAPI.IGridRequestType | any) => {
    const request: ManageCommonAPI.IGridRequestType | any = {
      ...otherRequest,
    };

    if (request?.singTime) {
      const [singTimeStart, singTimeEnd] = request.singTime;
      request['singTimeStart'] = singTimeStart.format('YYYY-MM-DD 00:00:00');
      request['singTimeEnd'] = singTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['singTime'];
    }

    if (request?.cancelTime) {
      const [cancelTimeStart, cancelTimeEnd] = request.cancelTime;
      request['cancelTimeStart'] = cancelTimeStart.format('YYYY-MM-DD 00:00:00');
      request['cancelTimeEnd'] = cancelTimeEnd.format('YYYY-MM-DD 23:59:59');
      delete request['cancelTime'];
    }

    delete request['signFormNameDept'];
    delete request['signFormNameUser'];
    if (request?.signerID && request?.signerID?.length > 0) {
      request['signerID'] = request?.signerID?.toString();
    } else {
      delete request['signerID'];
    }

    run({
      ...request,
    });
  };

  /** 搜索列表  */
  const searchList = useMemo(() => {
    return {
      searchList: searchListParams,
    };
  }, [loadMdicMapObj?.loadMapInfo]);

  /** 页面列表 */
  const returnTableProps = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  return {
    loading,
    getTempContractData,
    reloadRefresh: refresh,
    ...returnTableProps,
    ...searchList,
  };
};

export { templateManageService };
